/*
 * This is Melet, a salary slip designer. Feed it with data regarding to salary payment, prepare it for print through the graphical presentation, and you have it done.
 * Copyright (C) 2014, AFDiaX
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * This program is based on UMLet, a Free UML Tool for Fast UML Diagrams.
 * To contact the authors of UMLet visit https://code.google.com/p/umlet/people/list
 * To contact the authors of Melet visit https://gna.org/users/gabocze
 */
package com.baselet.element.elementnew.sueldos;

import java.awt.Image;
import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.List;

import javax.imageio.ImageIO;
import javax.imageio.ImageReader;

import com.baselet.control.SharedUtils;
import com.baselet.control.basics.geom.Rectangle;
import com.baselet.control.enums.ElementId;
import com.baselet.diagram.draw.DrawHandler;
import com.baselet.element.NewGridElement;
import com.baselet.element.draw.DrawHelper;
import com.baselet.element.facet.Facet;
import com.baselet.element.facet.PropertiesParserState;
import com.baselet.element.facet.Settings;
import com.baselet.element.interfaces.Component;
import com.baselet.element.interfaces.DrawHandlerInterface;
import com.baselet.element.settings.SettingsAutoresize;
import com.baselet.element.settings.SettingsManualResizeTop;
import com.baselet.element.sticking.StickingPolygon;
import com.baselet.element.sticking.polygon.StickingPolygonGenerator;

public class BackgroundImage extends NewGridElement {
	private Image image;
	// ElementFactorySwing nos pasa :Rectangle(x,y,w,h) de alguna manera
	public BackgroundImage() {
		try {
			String path = "..\\..\\AppData\\Plantilla Recibo.jpg";
			
			image = ImageIO.read(new File(path));
			//setRectangle(new Rectangle(0,0,image.getWidth(null), image.getHeight(null)));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	
	/* (non-Javadoc)
	 * @see com.baselet.element.NewGridElement#init(com.baselet.control.basics.geom.Rectangle, java.lang.String, java.lang.String, com.baselet.element.interfaces.Component, com.baselet.element.interfaces.DrawHandlerInterface)
	 */
	@Override
	public void init(Rectangle bounds, String panelAttributes,
			String additionalAttributes, Component component,
			DrawHandlerInterface handler) {
		super.init(new Rectangle(bounds.x, bounds.y,image.getWidth(null),image.getHeight(null)), panelAttributes, additionalAttributes, component, handler);
		
	}



	private final StickingPolygonGenerator backgroundImageStickingPolygonGenerator = new StickingPolygonGenerator() {
		@Override
		public StickingPolygon generateStickingBorder(Rectangle rect) {
			StickingPolygon p = new StickingPolygon(rect.x, rect.y);
			p.addPoint(0, 0);
			p.addPoint(getRealSize().width, 0);
			p.addPoint(getRealSize().width, getRealSize().height);
			p.addPoint(0, getRealSize().height, true);
			return p;
		}
	};

	@Override
	protected Settings createSettings() {
		return new SettingsManualResizeTop() {
			
			@Override
			protected List<? extends Facet> createFacets() {
				return Collections.EMPTY_LIST;
			}
		};
	}

	@Override
	public ElementId getId() {
		return ElementId.BackgroundImage;
	}

	@Override
	protected void drawCommonContent(DrawHandler drawer, PropertiesParserState state) {
		state.addToYPos(getRectangle().height);
		state.updateCalculatedElementWidth(getRectangle().width);

		drawer.drawImage(getRectangle().x, getRectangle().y, getRealSize().width, getRealSize().height, image);

		state.setStickingPolygonGenerator(backgroundImageStickingPolygonGenerator);
	}
}
